package org.unt.genlogic;

import java.io.IOException;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Https {

	public static Properties test() {

		Properties properties = new Properties();

		properties.setProperty("proxy.url", "http.internetpsa.inetpsa.com");
		properties.setProperty("proxy.port", "80");
		properties.setProperty("proxy.user", "u336654");
		properties.setProperty("proxy.password", "vago7930");

		properties.setProperty("target.host", "jip.inetpsa.com"); // jip.inetpsa.com svn2.informatique.inetpsa.com www.clarin.com
		//properties.setProperty("target.host", "www.clarin.com");
		properties.setProperty("target.port", "80");
		// properties.setProperty("target.path", "/sociedad/Vargas-Llosa-democracia-necesita-contrapoderes_0_899910110.html");
		properties.setProperty("target.path", "/fr/description.html"); // "/politica/avion-usar-Cristina-proximos-meses_0_778122391.html");
																									// //"/svn/ciq00/Etudes/trunk/ciq_sonar_bundle/README");
		properties.setProperty("http.type", "http"); // "https"

		return properties;
	}

	public static void main(String[] args) throws ClientProtocolException, IOException {

		Https https = new Https(Https.test());
		String res = https.getHttpOutput();

		System.out.println(res);

	}

	private Properties properties;

	public Https(Properties properties) {
		this.properties = properties;
	}

	public String getHttpOutput() throws ClientProtocolException, IOException {

		String proxyUrl = properties.getProperty("proxy.url");
		String proxyPort = properties.getProperty("proxy.port");
		String proxyUser = properties.getProperty("proxy.user");
		String proxyPassword = properties.getProperty("proxy.password");
		String targetHostStr = properties.getProperty("target.host");
		String targetPort = properties.getProperty("target.port");
		String targetPath = properties.getProperty("target.path");
		String httpType = properties.getProperty("http.type");

		DefaultHttpClient httpclient = new DefaultHttpClient();

		try {

			httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxyUrl, new Integer(proxyPort)),
					new UsernamePasswordCredentials(proxyUser, proxyPassword));

			HttpHost targetHost = new HttpHost(targetHostStr, new Integer(targetPort), httpType);
			HttpHost proxy = new HttpHost(proxyUrl, new Integer(proxyPort), httpType);

			httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
			//httpclient.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS, proxy);

			HttpGet httpget = new HttpGet(targetPath);

			HttpResponse response = httpclient.execute(targetHost, httpget);

			HttpEntity entity = response.getEntity();

			return EntityUtils.toString(entity);

		} finally {
			httpclient.getConnectionManager().shutdown();
		}
	}
}
